<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .bg{
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="bg" id="bg">
        <button id="login">登录</button>
    </div>
</body>
<script>
    var bg = document.getElementById("bg");
    var login = document.getElementById("login");

    login.onclick = function(e){
        //阻止事件冒泡的过程，事件执行到此元素，不再向上冒泡
        e.stopPropagation();
        console.log("按钮被点击了");
    }
    bg.onclick = function(){
        console.log("bg被点击了");
    }
    document.body.onclick = function(){
        console.log("body被点击了");
    }
</script>
</html>